<?
set_time_limit(100);
require_once("../config/dbconn.php");require_once("../config/powercls.php");


if($act=='chaigu'){
	$msg='';
	//if(trim($gpprice)=='') $msg.="请输入拆股金额\\n";
	/*else{
		if($glo_gpcurprice<=$gpprice){
			$msg.="拆股价格必须小于报单币价格\\n";
		}else{
		if ($glo_gpcurprice%$gpprice>0){
			$msg.="当前报单币价格必须是拆股金额的倍数\\n";
		}
		}
	}*/
	if ($msg!=''){
		echo "<script>alert('$msg');history.back();</script>";exit();
	}
	
	//更新价格和报单币数量
	$sqlgx1="update {$db_prefix}gupiaoset set gpcurprice='$glo_gp_kpprice',chaigunum=chaigunum+1,gpsaleoutnum1=0 where 1";
	$db->query($sqlgx1);
	//更新会员的报单币数量
	$sqlgx1="update {$db_prefix}users set gpnum=gpnum*2 where 1";
	$db->query($sqlgx1);
	//更新会员的报单币数量
	$sqlgx1="update {$db_prefix}gpautosaleout set chaigunum1=chaigunum1+1 where state=0";
	$db->query($sqlgx1);
	//拆股的时候
	$sqlgx1="update {$db_prefix}gp_market set num=num*2,price='$glo_gp_kpprice' where state=0 and type=2";
	$db->query($sqlgx1);
	$chaiguok=1;
	
	$gpmaxprice=$glo_gp_kpprice*2;$chaigunum=0;
	
	if ($chaiguok==1){
		//拆股了/////////////////////////
		//获取可以自动卖出的记录
		$curtradeallnum=0;
		$sqlauto="select * from {$db_prefix}gpautosaleout where state=0 and price<='$gpmaxprice' and chaigunum=chaigunum1";
		$resultauto=$db->query($sqlauto);
		while($rsauto=$db->fetch_array($resultauto)){
			$num=$rsauto['num']*pow(2,$rsauto['chaigunum']);
			$price=$rsauto['price'];
			////////////////////////////////////////////////////
			$sqlsaleout="insert into {$db_prefix}gp_market(username,num,num1,price,jztime,type,addtime,state) values('".$rsauto['username']."','".intval($num)."','".intval($num)."','".floatval($price)."','0','2','".$curtime."',0)";
			$db->query($sqlsaleout);
			$curmarketid=$db->insert_id();
			//更新自动卖出记录的状态
			$sqlgx2="update {$db_prefix}gpautosaleout set state=1,saletime='$curtime' where id='".$rsauto['id']."'";
			$db->query($sqlgx2);
			/////////////////////////////////////////////
			require("gptrading2.php");
			/////////////////////////////
		}
		$db->free_result($resultauto);
			
		if ($curtradeallnum>0){
			autosaleoutgpfunction($curtradeallnum);
		}
		//拆股了	///////////////////////////////////	
	}
	
	//设定股票自动开启
	$sqlgx="update {$db_prefix}salaryset set closegupiao=0 where 1";
	$db->query($sqlgx);
			
	echo "<script>alert('拆股成功');location.href='gupiao_chaigu.php';</script>";exit();
}

/////////////////////循环卖出////////////////////////////
function autosaleoutgpfunction($curtradeallnum){
	global $db,$db_prefix,$curtime,$glo_gp_scgupiaonum,$gpmaxprice;
	
	//获取当前的价格
	$sqlgpj="select gpcurprice,gpsaleoutsy from {$db_prefix}gupiaoset";
	$rsgpj=$db->get_one($sqlgpj);
	$curgpprice=$rsgpj['gpcurprice'];$gpsaleoutsy=$rsgpj['gpsaleoutsy'];
	
	//看股票已经卖出多少了
	//看股票的价格是否又要上涨了
	if($curtradeallnum>0){
		//看股票已经卖出多少了
		$chaiguok=0;
		$curgpsaleoutnum1=$gpsaleoutsy+intval($curtradeallnum);
		if ($curgpsaleoutnum1>=$glo_gp_scgupiaonum){
			$gupiaoscnum=floor($curgpsaleoutnum1/$glo_gp_scgupiaonum);
			$gpcurpriceup=$curgpprice+$gupiaoscnum*0.0001;
			$gpsaleoutsycur=$curgpsaleoutnum1%$glo_gp_scgupiaonum;
			if ($gpcurpriceup>$curgpprice){
				//股票价格上涨了
				$sqlgx="update {$db_prefix}gupiaoset set gpcurprice='".$gpcurpriceup."',gpsaleoutsy='$gpsaleoutsycur' where 1";
				$db->query($sqlgx);
			}
			//自动拆股
			if ($gpcurpriceup>=$glo_gp_kpprice*2){
				//关闭系统
				$sqlgb="update {$db_prefix}salaryset set closegupiao=1 where 1";
				$db->query($sqlgb);
				//更新价格和报单币数量
				/*$sqlgx1="update {$db_prefix}gupiaoset set gpcurprice='$glo_gp_kpprice',chaigunum=chaigunum+1,gpsaleoutnum1=0 where 1";
				$db->query($sqlgx1);
				//更新会员的报单币数量
				$sqlgx1="update {$db_prefix}users set gpnum=gpnum*2 where 1";
				$db->query($sqlgx1);
				//更新会员的报单币数量
				$sqlgx1="update {$db_prefix}gpautosaleout set chaigunum1=chaigunum1+1 where state=0";
				$db->query($sqlgx1);
				//拆股的时候
				$sqlgx1="update {$db_prefix}gp_market set num=num*2,price='$glo_gp_kpprice' where state=0 and type=1";
				$db->query($sqlgx1);
				$chaiguok=1;*/
			}
			///////////////////////	
		}
	}
	
	/*if ($chaiguok==1){
		//拆股了/////////////////////////
			//获取可以自动卖出的记录
			$curtradeallnum=0;
			$sqlauto="select * from {$db_prefix}gpautosaleout where state=0 and price<='$gpmaxprice' and chaigunum=chaigunum1";
			$resultauto=$db->query($sqlauto);
			while($rsauto=$db->fetch_array($resultauto)){
				$num=$rsauto['num']*pow(2,$rsauto['chaigunum']);
				$price=$rsauto['price'];
				////////////////////////////////////////////////////
				$sqlsaleout="insert into {$db_prefix}gp_market(username,num,num1,price,jztime,type,addtime,state) values('".$rsauto['username']."','".intval($num)."','".intval($num)."','".floatval($price)."','0','2','".$curtime."',0)";
				$db->query($sqlsaleout);
				$curmarketid=$db->insert_id();
				//更新自动卖出记录的状态
				$sqlgx2="update {$db_prefix}gpautosaleout set state=1,saletime='$curtime' where id='".$rsauto['id']."'";
				$db->query($sqlgx2);
				/////////////////////////////////////////////
				require("gptrading2.php");
				/////////////////////////////
			}
			$db->free_result($resultauto);
			
			if ($curtradeallnum>0){
				autosaleoutgpfunction($curtradeallnum);
			}
			//拆股了	///////////////////////////////////	
	}*/
	
	//获取当前的价格
	$sqlgpj="select gpcurprice,gpsaleoutsy from {$db_prefix}gupiaoset";
	$rsgpj=$db->get_one($sqlgpj);
	$curgpprice=$rsgpj['gpcurprice'];$gpsaleoutsy=$rsgpj['gpsaleoutsy'];
	
	//获取可以自动卖出的记录
	$curtradeallnum=0;
	$sqlauto="select * from {$db_prefix}gpautosaleout where state=0 and price<='$curgpprice' and chaigunum=chaigunum1";
	$resultauto=$db->query($sqlauto);
	while($rsauto=$db->fetch_array($resultauto)){
		$num=$rsauto['num']*pow(2,$rsauto['chaigunum']);
		$price=$rsauto['price'];
		////////////////////////////////////////////////////
		$sqlsaleout="insert into {$db_prefix}gp_market(username,num,num1,price,jztime,type,addtime,state) values('".$rsauto['username']."','".intval($num)."','".intval($num)."','".floatval($price)."','0','2','".$curtime."',0)";
		$db->query($sqlsaleout);
		$curmarketid=$db->insert_id();
		//更新自动卖出记录的状态
		$sqlgx2="update {$db_prefix}gpautosaleout set state=1,saletime='$curtime' where id='".$rsauto['id']."'";
		$db->query($sqlgx2);
		/////////////////////////////////////////////
		require("gptrading2.php");
		
		/////////////////////////////
	}
	$db->free_result($resultauto);
	
	if ($curtradeallnum>0){
		autosaleoutgpfunction($curtradeallnum);
	}
}
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style type="text/css">
<!--
body {
	margin-left: 0px;
	margin-top: 0px;
	margin-right: 0px;
	margin-bottom: 0px;
}
-->
</style>

<link href="images/skin.css" rel="stylesheet" type="text/css" />
<style type="text/css">
<!--
.STYLE1 {color: #FF0000}
-->
</style>
</head>
<body>
<table width="100%" border="0" cellpadding="0" cellspacing="0">
  <tr>
    <td width="17" height="29" valign="top" background="images/mail_leftbg.gif"><img src="images/left-top-right.gif" width="17" height="29" /></td>
    <td width="935" height="29" valign="top" background="images/content-bg.gif"><table width="100%" height="31" border="0" cellpadding="0" cellspacing="0" class="left_topbg" id="table2">
      <tr>
        <td height="31"><div class="titlebt">报单币拆股</div></td>
      </tr>
    </table></td>
    <td width="16" valign="top" background="images/mail_rightbg.gif"><img src="images/nav-right-bg.gif" width="16" height="29" /></td>
  </tr>
  <tr>
    <td height="71" valign="middle" background="images/mail_leftbg.gif">&nbsp;</td>
    <td valign="top" bgcolor="#F7F8F9"><table width="100%" height="138" border="0" cellpadding="0" cellspacing="0">
      <tr>
        <td height="13" valign="top">&nbsp;</td>
      </tr>
      <tr>
        <td valign="top">
		<form action="?act=chaigu" method="post" name="form1" id="form1" onClick="return confirm('确定拆股吗？')">
		<table width="100%" border="0" cellspacing="0" cellpadding="0">
            <tr>
              <td colspan="3"><table width="100%" height="31" border="0" cellpadding="0" cellspacing="0" class="nowtable">
                <tr>
                  <td class="left_bt2">&nbsp;&nbsp;&nbsp;&nbsp;</td>
                </tr>
              </table></td>
            </tr>
            <tr>
              <td height="30" colspan="3">
			  
			  <table width="100%" height="30" border="0" cellpadding="0" cellspacing="0">
                <tr>
                  <td width="48%" height="30" align="right" class="left_txt">报单币现在价格：</td>
                  <td width="52%" height="30" class="left_txt"><?=$glo_gpcurprice?>美金/股</td>
                </tr>
              </table></td>
            </tr>
            
            <tr>
              <td height="30" colspan="3">&nbsp;</td>
            </tr>
            <tr>
              <td width="50%" height="30" align="right"><input type="submit" value="确定拆股" name="B1" /></td>
              <td width="6%" height="30" align="right">&nbsp;</td>
              <td width="44%" height="30"></td>
            </tr>
            <tr>
              <td height="30" colspan="3">&nbsp;</td>
            </tr>
          </table>
		  </form>
		  </td>
      </tr>
    </table></td>
    <td background="images/mail_rightbg.gif">&nbsp;</td>
  </tr>
  <tr>
    <td valign="middle" background="images/mail_leftbg.gif"><img src="images/buttom_left2.gif" width="17" height="17" /></td>
      <td height="17" valign="top" background="images/buttom_bgs.gif"><img src="images/buttom_bgs.gif" width="17" height="17" /></td>
    <td background="images/mail_rightbg.gif"><img src="images/buttom_right2.gif" width="16" height="17" /></td>
  </tr>
</table>

</body>
</html>